//
//  UIKitModule.m
//  Home
//
//
//  Created by 胡锦涛 on 2021/1/5.
//

#import "ComponentsModule.h"
#import <HKLibBase/HKLibBaseHeader.h>
#import "HKComponentsViewController.h"

@implementation ComponentsModule
{
    // 是否是单独编译
    BOOL isSingleBuild;
}
+ (void)load {
    BFRegister(ComponentsModuleService);
}

#pragma mark - BifrostModuleProtocol
+ (instancetype)sharedInstance {
    static ComponentsModule *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (void)setup {
    isSingleBuild = [[[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleExecutableKey] isEqualToString:@"HKModuleComponents_Example"];
}

+ (NSUInteger)priority {
    return BifrostModuleDefaultPriority; //higher priority than other modules
}

+ (BOOL)setupModuleSynchronously {
    return YES;
}

#pragma mark - UIApplicationDelegate
#ifdef UIWindowScene_Enabled

#pragma mark - <UIWindowSceneDelegate>

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions API_AVAILABLE(ios(13.0)) {
    if (isSingleBuild) {
        if (@available(iOS 13.0, *)) {
            if ([scene isKindOfClass:UIWindowScene.class]) {
                [self didInitWindow:[[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene]];
            }
        }
    }
}

#endif

#pragma mark - <UIApplicationDelegate>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if (isSingleBuild) {
        // 1. 先注册主题监听，在回调里将主题持久化存储，避免启动过程中主题发生变化时读取到错误的值
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleThemeDidChangeNotification:) name:QMUIThemeDidChangeNotification object:nil];
        
        // 2. 然后设置主题的生成器
        QMUIThemeManagerCenter.defaultThemeManager.themeGenerator = ^__kindof NSObject * _Nonnull(NSString * _Nonnull identifier) {
            if ([identifier isEqualToString:HKThemeIdentifierDefault]) return QMUIConfigurationTemplate.new;
            if ([identifier isEqualToString:HKThemeIdentifierGrapefruit]) return QMUIConfigurationTemplateGrapefruit.new;
            if ([identifier isEqualToString:HKThemeIdentifierGrass]) return QMUIConfigurationTemplateGrass.new;
            if ([identifier isEqualToString:HKThemeIdentifierPinkRose]) return QMUIConfigurationTemplatePinkRose.new;
            if ([identifier isEqualToString:HKThemeIdentifierDark]) return QMUIConfigurationTemplateDark.new;
            if ([identifier isEqualToString:HKThemeIdentifierCustom]) return QMUIConfigurationCustom.new;
            return nil;
        };
        
        // 3. 再针对 iOS 13 开启自动响应系统的 Dark Mode 切换
        // 如果不需要这个功能，则不需要这一段代码
        if (@available(iOS 13.0, *)) {
            if (QMUIThemeManagerCenter.defaultThemeManager.currentThemeIdentifier) {// 做这个 if(currentThemeIdentifier) 的保护只是为了避免 HK 里的配置表没启动时，没人为 currentTheme/currentThemeIdentifier 赋值，导致后续的逻辑会 crash，业务项目里理论上不会有这种情况出现，所以可以省略这个 if，直接写下面的代码就行了
                
                QMUIThemeManagerCenter.defaultThemeManager.identifierForTrait = ^__kindof NSObject<NSCopying> * _Nonnull(UITraitCollection * _Nonnull trait) {
                    // 1. 如果当前系统切换到 Dark Mode，则返回 App 在 Dark Mode 下的主题
                    if (trait.userInterfaceStyle == UIUserInterfaceStyleDark) {
                        return HKThemeIdentifierDark;
                    }
                    
                    // 2. 如果没有命中1，说明此时系统是 Light，则返回 App 在 Light 下的主题即可，这里不直接返回 Default，而是先做一些复杂判断，是因为 QMUI Demo 非深色模式的主题有好几个，而我们希望不管之前选择的是 Default、Grapefruit 还是 PinkRose，只要从 Dark 切换为非 Dark，都强制改为 Default。
                    
                    // 换句话说，如果业务项目只有 Light/Dark 两套主题，则按下方被注释掉的代码一样直接返回 Light 下的主题即可。
                    //                return HKThemeIdentifierDefault;
                    
                    if ([QMUIThemeManagerCenter.defaultThemeManager.currentThemeIdentifier isEqual:HKThemeIdentifierDark]) {
                        return HKThemeIdentifierDefault;
                    }
                    return QMUIThemeManagerCenter.defaultThemeManager.currentThemeIdentifier;
                };
                QMUIThemeManagerCenter.defaultThemeManager.respondsSystemStyleAutomatically = YES;
            }
        }
        
        // QMUIConsole 默认只在 DEBUG 下会显示，作为 Demo，改为不管什么环境都允许显示
        [QMUIConsole sharedInstance].canShow = YES;
        
        // HK自定义的全局样式渲染
        [HKCommonUI renderGlobalAppearances];
        
        // 预加载 QQ 表情，避免第一次使用时卡顿
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [HKUIHelper qmuiEmotions];
        });
        
    #ifdef UIWindowScene_Enabled
        if (@available(iOS 13.0, *)) {
        } else {
    #endif
            // 界面
            application.delegate.window = [[UIWindow alloc] init];
            [self didInitWindow:application.delegate.window];
    #ifdef UIWindowScene_Enabled
        }
    #endif
        
    }
    
    
    return YES;
}

- (void)didInitWindow:(UIWindow *)window {
    UIViewController *homeVC = [Bifrost handleURL:kRouteComponentsPage];
    HKLibBaseNavController *rootNavContoller = [[HKLibBaseNavController alloc] initWithRootViewController:homeVC];
    window.rootViewController = rootNavContoller;
    window.backgroundColor = UIColorWhite;
    [window makeKeyAndVisible];
}

- (void)handleThemeDidChangeNotification:(NSNotification *)notification {
    
    QMUIThemeManager *manager = notification.object;
    if (![manager.name isEqual:QMUIThemeManagerNameDefault]) return;
    
    [[NSUserDefaults standardUserDefaults] setObject:manager.currentThemeIdentifier forKey:HKSelectedThemeIdentifier];
    
    [HKThemeManager.currentTheme applyConfigurationTemplate];
    
    // 主题发生变化，在这里更新全局 UI 控件的 appearance
    [HKCommonUI renderGlobalAppearances];
    
    // 更新表情 icon 的颜色
    [HKUIHelper updateEmotionImages];
}

#pragma mark - ComponentsModuleService

@end
